package com.example.nuc_ms_beta.ui.viewcomponent.slidmenuview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.example.nuc_ms_beta.R;


/**
 * Created by 张博川 on 2016/9/1.
 */
//横向滚动条View.实现侧滑菜单，加入  自定义实现距离屏幕右边的距离
public class LeftScrolSlideMenu extends HorizontalScrollView {
    private LinearLayout Wapper;
    private ViewGroup menu;
    private ViewGroup content;
    private int ScreenWidth;
    private int MenuRightRadding;
    public static boolean isOpen = false;
    private Context context;
    private boolean once = false;
    private int MenuWidth;
    private int ActionX;


    public LeftScrolSlideMenu(Context context) {
        this(context, null);
    }

    public LeftScrolSlideMenu(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public LeftScrolSlideMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LeftSlideMenu, defStyleAttr, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.LeftSlideMenu_rightPadding:
                    MenuRightRadding = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, this.context.getResources().getDisplayMetrics()));
                    break;
            }
        }
        a.recycle();
        WindowManager manager = (WindowManager) this.context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(metrics);
        ScreenWidth = metrics.widthPixels;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!once) {
            Wapper = (LinearLayout) getChildAt(0);
            menu = (ViewGroup) Wapper.getChildAt(0);
            content = (ViewGroup) Wapper.getChildAt(1);
            MenuWidth = menu.getLayoutParams().width = ScreenWidth - MenuRightRadding;
            content.getLayoutParams().width = ScreenWidth;
            once = true;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            this.scrollTo(MenuWidth, 0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
//        Log.e("86 LeftSlideMeny", "" + ev.getX());
        //设置响应滑动范围(目前设置20dp)
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                ActionX = (int) ev.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                if (ActionX < 30 || isOpen == true) {
                    break;
                } else {
                    return false;
                }
            case MotionEvent.ACTION_UP:
                int scrollX = getScrollX();
                if (scrollX >= MenuWidth / 2) {
                    this.smoothScrollTo(MenuWidth, 0);
                    isOpen = false;
                } else {
                    this.smoothScrollTo(0, 0);
                    isOpen = true;
                }
                return true;

        }
        return super.onTouchEvent(ev);
    }

    public void OpenMenu() {
        if (isOpen) return;
        this.smoothScrollTo(0, 0);
        isOpen = true;
    }


    public void CloseMenu() {
        if (!isOpen) return;
        this.smoothScrollTo(MenuWidth, 0);
        isOpen = false;
    }

    public void OpenORCLose() {
//        if (isOpen) {
//            CloseMenu();
//        } else {
//            OpenMenu();
//        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
    }


}